/**
 * Created by hjx on 2017/10/24.
 */
(function () {
    var exports = this;
    var g_idx = 0;
    var lastFlipTime = 0;
    var guide_png_array = [
        res.club_guide_1,
        res.club_guide_2,
        res.club_guide_3,
        res.club_guide_4,
    ];
    var guideInterval = null;
    var javaGuideLayer = cc.Layer.extend({
        ctor: function () {
            this._super();
            var that = this;

            cc.log('这里执行了啊 。。。。。');
            var btn_close = new cc.Sprite(res.btn_close);//activity/close_activity
            btn_close.x = display.width - 100;
            btn_close.y = display.height - 60;
            btn_close.setScale(1.5);
            this.addChild(btn_close,50);
            btn_close.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.3, 1.5), cc.scaleTo(0.3, 1.7))));
            TouchUtil.setOnclickListener(btn_close, function () {
                that.removeFromParent();
            });

            var modalLayer = new ccui.Layout();
            modalLayer.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
            modalLayer.setBackGroundColor(cc.color("#1A1A1A"));
            modalLayer.setSwallowTouches(true);
            modalLayer.setTouchEnabled(true);
            modalLayer.setContentSize(cc.winSize);
            modalLayer.setAnchorPoint(cc.p(0.5, 0.5));
            modalLayer.x = display.width / 2;
            modalLayer.y = display.height / 2;
            modalLayer.ignoreAnchorPointForPosition(false);
            modalLayer.setBackGroundColorOpacity(Math.floor(255 * 0.6));
            this.addChild(modalLayer);
            guideInterval = null;


            var widgetSize = cc.size(display.width, display.height);
            var background = cc.size(1560, 720);
            cc.sys.localStorage.setItem('guideClub', new Date().getTime());
            // Create the page view
            var pageView = new ccui.PageView();
            pageView.setTouchEnabled(true);
            var SAwidth  = display.width ;
            if (display.height / display.width > 0.56){
                SAwidth = display.width
            }
            pageView.setContentSize(cc.size(SAwidth, display.height));
            pageView.x = (widgetSize.width - background.width) / 2 + (background.width - pageView.width) / 2;
            pageView.y = (widgetSize.height - background.height) / 2 + (background.height - pageView.height) / 2;
            
            for (var i = 0; i < guide_png_array.length; ++i) {
                var layout = new ccui.Layout();
                layout.setContentSize(cc.size(display.width, display.height));
                var layoutRect = layout.getContentSize();

                var imageView = new ccui.ImageView();
                imageView.setTouchEnabled(false);
                imageView.setScale9Enabled(false);
                imageView.loadTexture(guide_png_array[i]);
                // imageView.setContentSize(cc.size(display.width, display.height));
                imageView.setScaleX(display.width / 1560);
                imageView.setScaleY(display.height / 720);
                imageView.x = display.width / 2;
                imageView.y = display.height / 2;

                layout.addChild(imageView);

                pageView.addPage(layout);
            }
            this.addChild(pageView);



            var btn_left = new cc.Sprite(res.club_guide_left);
            btn_left.x = 90;
            btn_left.y = 100;
            btn_left.setFlippedX(true);
            this.addChild(btn_left);
            btn_left.setVisible(false);
            TouchUtil.setOnclickListener(btn_left, function () {
                g_idx = parseInt(pageView.getCurPageIndex().valueOf());
                if (g_idx > 0) {
                    pageView.scrollToPage(g_idx - 1);
                    btn_left.setVisible(false);
                }
            });

            var btn_right = new cc.Sprite(res.club_guide_right);
            btn_right.x = 1190;
            btn_right.y = 90;
            btn_right.setScale(0.7);
            btn_right.setFlippedX(true);
            btn_right.runAction(cc.repeatForever(
                cc.sequence(
                    cc.moveTo(0.8, cc.p(990, 90)).easing(cc.easeIn(3)),
                    cc.fadeOut(0.5),
                    cc.callFunc(function () {
                        btn_right.x = 1190;
                        btn_right.y = 90;
                    }),
                    cc.moveTo(0.01, cc.p(1190, 90)),
                    cc.fadeIn(0.5)
                )
            ));
            this.addChild(btn_right);
            TouchUtil.setOnclickListener(btn_right, function () {
                g_idx = parseInt(pageView.getCurPageIndex().valueOf())
                if (g_idx < guide_png_array.length - 1) {
                    pageView.scrollToPage(g_idx + 1);
                    btn_right.setVisible(false);
                }
            });
            pageView.addEventListener(function (sender, type) {
                switch (type) {
                    case ccui.PageView.EVENT_TURNING:
                        var pageView = sender;
                        // this._topDisplayLabel.setString("page = " + (pageView.getCurPageIndex().valueOf() - 0 + 1));
                        g_idx = parseInt(pageView.getCurPageIndex().valueOf())
                        lastFlipTime = new Date().getTime();
                        break;
                    default:
                        break;
                }
            }, that);

            //自动翻转
            guideInterval = setInterval(function () {
                if (!cc.sys.isObjectValid(pageView)) {
                    clearInterval(guideInterval);
                    guideInterval = null;
                }
                var off = new Date().getTime() - lastFlipTime;
                console.log(off + "   " + g_idx);
                g_idx = parseInt(pageView.getCurPageIndex().valueOf())
                if (off >= 4500) {
                    if (g_idx < guide_png_array.length - 1) {
                        pageView.scrollToPage(g_idx + 1);
                    } else {
                        //pageView.scrollToPage(0);
                    }
                }
            }, 3500);
            return true;
        },
        onExit: function () {
            this._super();
            if (guideInterval) {
                clearInterval(guideInterval);
                guideInterval = null;
            }
        }
    });
    exports.javaGuideLayer = javaGuideLayer;
})(window);